
package com.huizhan.web.controller;
 
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.*;
import java.util.*;

import com.huizhan.web.dao.*;
import com.huizhan.web.model.*;

/**
 * 项目类型权限api
 */
 
@Api(tags = "ProjectTypeAuthorize",description = "项目类型权限")
@RestController
public class ProjectTypeAuthorizeController {
    
    @Autowired
    private ProjectTypeAuthorizeDao dao;
   
    /**
     * 获取项目类型权限集合
     * @return api结果
     */
    @ApiOperation(value = "获取项目类型权限集合")
    @RequestMapping(value =  "/api/ProjectTypeAuthorize/",method = {RequestMethod.GET})
    public ApiResult<List<ProjectTypeAuthorize>> Get(){
         ApiResult<List<ProjectTypeAuthorize>> result=new ApiResult<List<ProjectTypeAuthorize>>();
        try 
        {
            List<ProjectTypeAuthorize> res = dao.findAll();
            result.setData(res);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据ID获取项目类型权限信息
     * @param id 项目类型权限ID
     * @return api结果
     */
    @ApiOperation(value = "根据ID获取项目类型权限信息")
    @RequestMapping(value =  "/api/ProjectTypeAuthorize/{id}",method = {RequestMethod.GET})
    public ApiResult<ProjectTypeAuthorize> Get(@PathVariable Long id){
        ApiResult<ProjectTypeAuthorize> result=new ApiResult<ProjectTypeAuthorize>();
        try
        {
            ProjectTypeAuthorize model= dao.getOne(id);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据项目类型ID获取项目类型权限集合
     * @param id 项目类型权限ID
     * @return api结果
     */
    @ApiOperation(value = "根据项目类型ID获取项目类型权限集合")
    @RequestMapping(value =  "/api/ProjectTypeAuthorize/ProjectType/{projectTypeId}",method = {RequestMethod.GET})
    public ApiResult<List<ProjectTypeAuthorize>> ProjectType(@PathVariable Long projectTypeId){
        ApiResult<List<ProjectTypeAuthorize>> result=new ApiResult<List<ProjectTypeAuthorize>>();
        try
        {
            List<ProjectTypeAuthorize> model= dao.findByProjectTypeId(projectTypeId);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 修改项目类型权限信息
     * @param id 项目类型权限ID
     * @param model 项目类型权限信息
     * @return api结果
     
    @ApiOperation(value = "修改项目类型权限信息")
    @RequestMapping(value =  "/api/ProjectTypeAuthorize/{id}",method = {RequestMethod.PUT})
    public ApiResult<ProjectTypeAuthorize> Edit(@PathVariable Long id,@RequestBody ProjectTypeAuthorize model){
        ApiResult<ProjectTypeAuthorize> result=new ApiResult<ProjectTypeAuthorize>();
        try
        {
            model.setProjectTypeAuthorizeId(id);
            dao.save(model);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }
    */
    /**
     * 添加项目类型权限信息
     * @param model 项目类型权限信息
     * @return api结果
     */
    @ApiOperation(value = "添加项目类型权限信息")
    @RequestMapping(value =  "/api/ProjectTypeAuthorize/",method = {RequestMethod.POST})
    public ApiResult<ProjectTypeAuthorize> Add(@RequestBody ProjectTypeAuthorize model){
         ApiResult<ProjectTypeAuthorize> result=new ApiResult<ProjectTypeAuthorize>();
        try
        {
            dao.save(model);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 保存类型关联权限
     * @param projectTypeId 类型ID
     * @param model 角色权限信息
     * @return api结果
     */
    @ApiOperation(value = "保存类型关联权限")
    @RequestMapping(value =  "/api/ProjectTypeAuthorize/{projectTypeId}",method = {RequestMethod.PUT})
    public ApiResult<Object> Edit(@PathVariable Long projectTypeId,@RequestBody List<ProjectTypeAuthorize> model){
        ApiResult<Object> result=new ApiResult<Object>();
        try
        {
            dao.deleteByProjectTypeId(projectTypeId);

            for (ProjectTypeAuthorize p : model) {
                p.setProjectTypeId(projectTypeId);
            }
            
            dao.saveAll(model);
            
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }
}

